﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ComTools
{
    public partial class frmMySqlDBManager : Form
    {
        public frmMySqlDBManager()
        {
            InitializeComponent();
        }

        MySqlHelper mysqlHelper = null;
        string errmsg = string.Empty;

        private void btnConnect_Click(object sender, EventArgs e)
        {
            if (mysqlHelper == null)
            {
                mysqlHelper = new MySqlHelper();
            }
            int port = 3306;
            if (!int.TryParse(txtPort.Text.Trim(), out port))
            {
                MessageBox.Show("端口号不正确");
                return;
            }
            mysqlHelper.SetConn(txtServer.Text.Trim(), port,
                txtDataBase.Text.Trim(), txtUID.Text.Trim(), txtPwd.Text.Trim());
            //
            if (!mysqlHelper.TestDB(out errmsg))
            {
                MessageBox.Show(errmsg);
                return;
            }
            //
            tabControl1.Enabled = true;
        }

        private void tsbShowDataBase_Click(object sender, EventArgs e)
        {
            string dbname = txtDataBase.Text.Trim();
            DataTable dt = mysqlHelper.ShowDataBases(out errmsg);
            dgvDataBases.DataSource = dt;
            foreach (DataGridViewRow dgvr in dgvDataBases.Rows)
            {
                DataRowView drv = dgvr.DataBoundItem as DataRowView;
                if (drv[0].ToString().Equals(dbname, StringComparison.OrdinalIgnoreCase))
                {
                    dgvr.Selected = true;
                }
                else
                {
                    dgvr.Selected = false;
                }
            }
        }

        private void tsbShowTables_Click(object sender, EventArgs e)
        {
            DataTable dt = null;
            string dbname = tsbKeyValue.Text.Trim();
            if (string.IsNullOrWhiteSpace(dbname))
            {
                dt = mysqlHelper.ShowTables(out errmsg);
            }
            else
            {
                dt = mysqlHelper.ShowTables(dbname, out errmsg);
            }
            dgvTables.DataSource = dt;
        }

        private void tsbShowColumns_Click(object sender, EventArgs e)
        {
            string tableName = tsbKeyValue.Text.Trim();
            if (string.IsNullOrWhiteSpace(tableName))
                return;
            DataTable dt = mysqlHelper.ShowColumns(tableName, out errmsg);
            dgvColumns.DataSource = dt;
        }

        private ICSharpCode.TextEditor.TextEditorControlEx textEditorControlEx1;

        private void frmDBManager_Load(object sender, EventArgs e)
        {
            tabControl1.Enabled = false;
            // 
            // textEditorControlEx1
            // 
            this.textEditorControlEx1 = new ICSharpCode.TextEditor.TextEditorControlEx();
            this.tabPage2.Controls.Add(this.textEditorControlEx1);
            this.textEditorControlEx1.Dock = System.Windows.Forms.DockStyle.Fill;
            this.textEditorControlEx1.Font = new System.Drawing.Font("Courier New", 10F);
            this.textEditorControlEx1.Location = new System.Drawing.Point(3, 3);
            this.textEditorControlEx1.Name = "textEditorControlEx1";
            this.textEditorControlEx1.SyntaxHighlighting = "Java";
            this.textEditorControlEx1.Document.HighlightingStrategy = ICSharpCode.TextEditor.Document.HighlightingStrategyFactory.CreateHighlightingStrategy("Java");
        }

        private void tsbConvert2Module_Click(object sender, EventArgs e)
        {
            string tableName = tsbKeyValue.Text.Trim();
            if (string.IsNullOrWhiteSpace(tableName))
                return;
            DataTable dt = mysqlHelper.ShowColumns(tableName, out errmsg);
            string className = mysqlHelper.GetClassName(tableName);

            string module = mysqlHelper.Columns2Module(dt, GetProjectPrefix() + "model." + className);
            textEditorControlEx1.Text = module;
            tabControl1.SelectedTab = tabPage2;
        }
        string[] modes = new string[] { "ASP3/XHTML", "BAT", "Boo", "Coco", "C++.NET", "C#", "HTML", "Java", "JavaScript", "PHP", "TeX", "VBNET", "XML", "TSQL" };

        private string GetProjectPrefix()
        {
            string txt = txtProjectPrefix.Text.Trim();
            if (!txt.EndsWith("."))
            {
                txtProjectPrefix.Clear();
                txtProjectPrefix.AppendText(txt + ".");
            }
            return txtProjectPrefix.Text.Trim();
        }

        private void dgvDataBases_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                var obj = dgvDataBases.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                //if (obj != null && obj != DBNull.Value)
                //    tsbKeyValue.Text = obj.ToString();
                //else
                //    tsbKeyValue.Clear();
                //
                string dbname = obj.ToString();
                DataTable dt = null;
                if (string.IsNullOrWhiteSpace(dbname))
                {
                    dt = mysqlHelper.ShowTables(out errmsg);
                }
                else
                {
                    dt = mysqlHelper.ShowTables(dbname, out errmsg);
                }
                dgvTables.DataSource = dt;
            }
        }

        private void dgvTables_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                var obj = dgvTables.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                if (obj != null && obj != DBNull.Value)
                    tsbKeyValue.Text = obj.ToString();
                else
                    tsbKeyValue.Clear();
                //tsbShowColumns.PerformClick();
                string tableName = obj.ToString();
                if (string.IsNullOrWhiteSpace(tableName))
                    return;
                DataTable dt = mysqlHelper.ShowColumns(tableName, out errmsg);
                dgvColumns.DataSource = dt;
            }
        }

        private void dgvColumns_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1 && e.ColumnIndex > -1)
            {
                var obj = dgvDataBases.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;
                if (obj != null && obj != DBNull.Value)
                    tsbKeyValue.Text = obj.ToString();
                else
                    tsbKeyValue.Clear();
                //
                tsbConvert2Module.PerformClick();
            }
        }

        private void tsbConvert2MapperXML_Click(object sender, EventArgs e)
        {
            string tableName = tsbKeyValue.Text.Trim();
            if (string.IsNullOrWhiteSpace(tableName))
                return;
            string className = mysqlHelper.GetClassName(tableName);
            string fullModuleName = GetProjectPrefix() + "model." + className;
            string fullMapperName = GetProjectPrefix() + "dao." + className + "Mapper";
            string mapper = mysqlHelper.Table2MapperXml(tableName, fullMapperName, fullModuleName);
            textEditorControlEx1.Text = mapper;
            tabControl1.SelectedTab = tabPage2;
        }

        private void ToMapperInt_Click(object sender, EventArgs e)
        {
            string tableName = tsbKeyValue.Text.Trim();
            if (string.IsNullOrWhiteSpace(tableName))
                return;
            string className = mysqlHelper.GetClassName(tableName);
            string fullModuleName = GetProjectPrefix() + "model." + className;
            string fullMapperName = GetProjectPrefix() + "dao." + className + "Mapper";
            //string fullMapperName = className + "Mapper";
            DataTable dt = mysqlHelper.ShowColumns(tableName, out errmsg);
            //string mapper = mysqlHelper.Table2MapperInterface(dt, fullModuleName + "Mapper", fullModuleName);
            string mapper = mysqlHelper.Table2MapperInterface(dt, fullMapperName, fullModuleName);
            textEditorControlEx1.Text = mapper;
            tabControl1.SelectedTab = tabPage2;
        }

        private void txtDBFilter_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = dgvDataBases.DataSource as DataTable;
            if (dt == null) return;
            dt.DefaultView.RowFilter = $"database like '%{txtDBFilter.Text.Trim()}%'";
        }

        private void txtTableFilter_TextChanged(object sender, EventArgs e)
        {
            DataTable dt = dgvTables.DataSource as DataTable;
            if (dt == null) return;
            dt.DefaultView.RowFilter = $"table like '%{txtTableFilter.Text.Trim()}%'";
        }

        private void cmbColumns_SelectedIndexChanged(object sender, EventArgs e)
        {
            FilterColumns();
        }

        private void txtColumnFilter_TextChanged(object sender, EventArgs e)
        {
            FilterColumns();
        }

        private void FilterColumns()
        {
            DataTable dt = dgvColumns.DataSource as DataTable;
            if (dt == null) { return; }
            if (cmbColumns.SelectedIndex == -1) { return; }
            string column = cmbColumns.SelectedItem.ToString();
            dt.DefaultView.RowFilter = $"{column} like '%{txtColumnFilter.Text.Trim()}%'";
        }
    }
}